﻿using System;
using System.IO;
using System.Drawing;
using System.Windows.Forms;


namespace Serial_assistant
{
    [System.Runtime.InteropServices.ComVisible(true)]
    public partial class BMaps : Form
    {
        //public string ewhemi = "N", nshemi = "E";           //南北纬，东西经
        public double pre_longitude, pre_latitude;//记录上一时刻经纬度位置
        public double longitude, latitude;//记录当前时刻经纬度位置
        public ShowWindow ShowMainWindow;//定义显示主窗口委托访问权限为public
        public HideWindow HideMainWindow;//定义隐藏主窗口委托
        public OpenPort OpenSerialPort;//定义打开串口委托
        public ClosePort CloseSerialPort;//定义关闭串口委托
        public GetMainPos GetMainPos;//定义获取主窗口信息委托(自动对齐)
        public GetMainWidth GetMainWidth;//定义获取主窗口宽度(自动对齐)
        public bool isfirst = true;
        public double t = 0.001;
        public BMaps()
        {
            InitializeComponent();
        }

        private void BMaps_Load(object sender, EventArgs e)
        {
            try
            {
                webBrowser1.Url = new Uri(Path.Combine(Application.StartupPath, "map.html"));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "异常", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            //webBrowser1.Navigate("file:///C:/Users/86159/Desktop/WindowsFormsApp1/WindowsFormsApp1/bin/Release/map.html");

        }

        //设置创建窗口时的位置函数
        public void SetWindow(Point Pt)//允许主窗口设置自己
        {
            this.Location = Pt;
        }

        public void DrawLine()
        {
            //调用js函数实现地图上画线
            webBrowser1.Document.InvokeScript("drawGreenLine", new object[]
            { (float)this.pre_longitude, (float)this.pre_latitude, (float)this.longitude, (float)this.latitude});

            //textBox1.Text = t.ToString();
            //t *= 1.1;
            //更新上一时刻经纬度数据
            this.pre_longitude = this.longitude;
            this.pre_latitude = this.latitude;
        }

        public void update()
        {
            this.button2_Click(null, null);
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        public void show_location(double longitude, string ewhemi, double latitude, string nshemi, double altitude, double speed)
        {
            this.textBox1.Text = "纬度 : " + nshemi + "  " + latitude.ToString() + System.Environment.NewLine +
                                 "精度 : " + ewhemi + "  " + longitude.ToString() + System.Environment.NewLine +
                                 "海拔高度 :  " + altitude.ToString() + System.Environment.NewLine +
                                 "运动速度 :  " + speed.ToString() + System.Environment.NewLine;
        }

        public void button2_Click(object sender, EventArgs e)
        {
            DrawLine();
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                webBrowser1.Document.InvokeScript("lineplay", new object[] { });
            }
            catch
            {

            }
        }

        private void Drawer_FormClosing(object sender, FormClosingEventArgs e)
        {
            ShowMainWindow();//关闭自己，显示主窗口
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                webBrowser1.Document.InvokeScript("setLocation", new object[] { this.longitude, this.latitude });
            }
            catch
            {

            }
        }

    }
}
